/*:
 * @plugindesc (v1.00)连续状态
 * @author 小c
 * 
 * @help 
 * 创建连续出现不同效果的状态。
 * 即状态X解除时马上添加状态Y
 * 若使用了CP_StateFloor，请将该插件放在它的下面
 * 
 * 状态标签
 * <Consecutive State: x>
 * 本状态结束时马上会添加的状态，x为状态id
 * 注意：在满足连续状态条件时，任何形式的解除状态X均会添加状态Y
 * 但战斗结束结算时的解除状态除外
 *
 * 连续状态条件
 * 状态标签
 * <Consecutive State Condition>
 * //JavaScript code
 * condition = this.isStateAffected(20);
 * </Consecutive State Condition>
 * 指定连续状态的条件，赋值给condition变量
 * 若condition = true时满足触发条件，this指向当前角色
 * 若没有配置此项或者此项出现JS错误，则无条件满足触发条件
 *
 * 举例：给一个状态X如下配置
 * <Consecutive State: 5>
 * <Consecutive State Condition>
 * //JavaScript code
 * condition = this.isStateAffected(4);
 * </Consecutive State Condition>
 * 以上表示状态X解除时，若角色带有状态4，则添加状态5
 * 
 * 更新日志
 * v1.00
 * 插件完成
 */

var Imported = Imported || {};
Imported.CP_ConsecutiveState = true;

var CP = CP || {};
CP.ConsecutiveStateManager = CP.ConsecutiveStateManager || {};

CP.ConsecutiveStateManager._loaded = false;
CP.ConsecutiveStateManager.DATABASE_LOADED = DataManager.isDatabaseLoaded;
DataManager.isDatabaseLoaded = function(){
	if(!CP.ConsecutiveStateManager.DATABASE_LOADED.call(this))
		return false;
	if(!CP.ConsecutiveStateManager._loaded){
		CP.ConsecutiveStateManager.loadConsecutiveStates();
		CP.ConsecutiveStateManager._loaded = true;
	}
	return true;
};

CP.ConsecutiveStateManager.loadConsecutiveStates = function(){
	var length = $dataStates.length;
	var reg = /<Consecutive State Condition>([\s\S]*)<\/Consecutive State Condition>/;
	for(var i = 1; i < length; i++){
		var state = $dataStates[i];
		state.consecutiveState = 0;
		state.consecutiveStateCondition = null;
		if(state.meta["Consecutive State"])
			state.consecutiveState = Number(state.meta["Consecutive State"]) || 0;
		if(state.consecutiveState < 0 || state.consecutiveState >= length)
			state.consecutiveState = 0;
		if(state.consecutiveState > 0 && reg.exec(state.note))
			state.consecutiveStateCondition = RegExp.$1;
	}
};

//-------------------------------------------
// Game_Battler
//-------------------------------------------
CP.ConsecutiveStateManager.BATTLER_REMOVE_BATTLE_STATES = Game_Battler.prototype.removeBattleStates;
Game_Battler.prototype.removeBattleStates = function(){
    this._stateRemoveEndBattleFlag = true;
    CP.ConsecutiveStateManager.BATTLER_REMOVE_BATTLE_STATES.call(this);
    this._stateRemoveEndBattleFlag = false;
};

CP.ConsecutiveStateManager.BATTLER_REMOVE_STATE = Game_Battler.prototype.removeState;
Game_Battler.prototype.removeState = function(stateId){
    var affected1 = this.isStateAffected(stateId);
    CP.ConsecutiveStateManager.BATTLER_REMOVE_STATE.call(this, stateId);
    var affected2 = this.isStateAffected(stateId);

    this.addConsecutiveState(stateId, affected1, affected2);
};

Game_Battler.prototype.addConsecutiveState = function(baseStateId, oldAffected, newAffected){
	var baseState = $dataStates[baseStateId];
	if(this.meetsAddConsecutiveStateCondition(baseState) && oldAffected && !newAffected){
		this.addState(baseState.consecutiveState);
		this.refresh();
	}
};

Game_Battler.prototype.meetsAddConsecutiveStateCondition = function(state){
	if(!state)
		return false;
	else if(!!this._stateRemoveEndBattleFlag) //战斗结算中
		return false;
	else if(state.consecutiveState <= 0)
		return false;
	return this.meetsAddConsecutiveStateConditionEval(state);
};

Game_Battler.prototype.meetsAddConsecutiveStateConditionEval = function(state){
	var condition = true;
	try{
		if(!!state.consecutiveStateCondition)
			eval(state.consecutiveStateCondition);
	}catch(err){
		console.error(err);
		condition = true;
	}
	return condition;
};